/*
 * ChooseBankDialog.java
 * business
 *
 * Created by SunDing on 2017/2/22.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.ui.adapter.ChooseBankAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择开户银行Dialog
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/2/22 11:33
 */
public class ChooseBankDialog extends Dialog implements View.OnClickListener {

    private ImageView mIvClose;
    private TextView mTvFinish;
    private ListView mListView;
    private OnChooseBankListener mListener;
    private ChooseBankAdapter mAdapter;

    public ChooseBankDialog(Context context) {
        super(context, R.style.BottomDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_bank);

        initViews();
        initWindow();
        initListeners();
    }

    private void initListeners() {
        mIvClose.setOnClickListener(this);
        mTvFinish.setOnClickListener(this);
    }

    private void initWindow() {
        // 改变样式-->window的样式
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        // 设置弹出位置
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;

        window.setAttributes(params);
    }

    private void initViews() {
        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mListView = (ListView) findViewById(R.id.list_view);

        mAdapter = new ChooseBankAdapter(getContext());
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_finish:
                dismiss();
                if (mListener != null) {
                    mListener.OnChooseBank();
                }
                break;
        }
    }

    @Override
    public void show() {
        super.show();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               /* // 设置选中项
                PayWay selectedPayWay = ((PayWayListItem) parent.getItemAtPosition(position))
                .getContentData();
                choosePayWay(selectedPayWay);
                mAdapter.notifyRecyclerViewDataSetChanged();*/
            }
        });
    }

    public void choosePayWay(PayWay selectedPayWay) {

       /* List<DisplayListItem> payWays = mAdapter.getData();
        for (DisplayListItem payWayItem :payWays){
            PayWay payWay = ((PayWayListItem) payWayItem).getContentData();
            payWay.setSelected(selectedPayWay.equals(payWay));
        }

        mPayWay = selectedPayWay;*/
    }

    public void setOnChooseBankListener(OnChooseBankListener listener) {
        mListener = listener;
    }

    public void setBankCardData() {
        List<DisplayListItem> list = new ArrayList<>();
        mAdapter.setData(list);
    }

    public interface OnChooseBankListener {

        void OnChooseBank();
    }
}
